package com.example.origin.spider.demo;

import lombok.SneakyThrows;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HttpClientDemo {
    public static void main(String[] args) {

        PoolingHttpClientConnectionManager cm = getConnectionManager();

        RequestConfig config = getRequestConfig();

//        post(cm,config);
        get(cm,config);
    }

    private static RequestConfig getRequestConfig() {
        //配器请求信息
        RequestConfig config= RequestConfig.custom().setConnectTimeout(1000)//创建连接的最长时间，单位是毫秒
                .setConnectionRequestTimeout(500)//设置获取连接的最长时间，单位是毫秒
                .setSocketTimeout(10*1000) //设置数据传输的最长时间，单位是豪秒
                 .build();
        return config;
    }

    private static PoolingHttpClientConnectionManager getConnectionManager() {
        //创建连接池管理器
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        //设置最大连接数
        cm.setMaxTotal(100);
        //设置每个主机的最大连接数
        cm.setDefaultMaxPerRoute(10);
        return cm;
    }

    @SneakyThrows
    private static void post(PoolingHttpClientConnectionManager cm, RequestConfig config) {
        //创建HttpC1ient对象
        CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();
        //创建HttpGet,对象，设置urI访问地址
        HttpPost httpPost = new HttpPost("https://www.baidu.com");
        httpPost.setConfig(config);


        //声明List集合，封装表单中的参数
        List<NameValuePair> params = new ArrayList<>();
        //设置请求地址是：http:/yun.itheima.com/search?keys=Java
        params.add(new BasicNameValuePair("keys","Java"));
        //创建表单的tity对象，第一个参数就是封装好的表单数据，第二个参数就是编码
        UrlEncodedFormEntity formEntity  = new UrlEncodedFormEntity(params,"utf8");
        //设置表单的Entityi对象到Post请求中
        httpPost.setEntity(formEntity);

        try (//使用Httpclient发起请求，获取response
             CloseableHttpResponse response = httpclient.execute(httpPost)) {
            //解析响应
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "utf8");
                System.out.println(content);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void get(PoolingHttpClientConnectionManager cm, RequestConfig config) {
        //创建HttpC1ient对象
        CloseableHttpClient httpclient = HttpClients.custom()/*.setConnectionManager(cm)*/.build();
        //创建HttpGet,对象，设置urI访问地址
        HttpGet httpGet = new HttpGet("https://www.creditchina.gov.cn/");
//        httpGet.setConfig(config);
        try (//使用Httpclient发起请求，获取response
             CloseableHttpResponse response = httpclient.execute(httpGet)) {
            Header[] headers = response.getHeaders("Set-Cookie");
            System.out.println("dddd::"+headers[0].getValue());
            //解析响应
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "utf8");
                //System.out.println(content);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
